﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Nirvana
{
    /// <summary>
    /// 用于控制AssetID脚本的变量如何显示
    /// </summary>
    [CustomPropertyDrawer(typeof(AssetID))]
    public class AssetIDPropertyDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect pos, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(pos, label, property);
            EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), label);
            EditorGUI.indentLevel++;
            var refRect = new Rect(pos.x, pos.y + EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var bundlerRect = new Rect(pos.x, pos.y + 3*EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var assetRect = new Rect(pos.x, pos.y + 2*EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            SerializedProperty bundlename = property.FindPropertyRelative("bundlename");
            SerializedProperty assetname = property.FindPropertyRelative("assetname");
            SerializedProperty assetguiid = property.FindPropertyRelative("assetguiid");
            Type type = typeof(Object);
            object[] customAttributes = base.fieldInfo.GetCustomAttributes(typeof(AssetTypeAttribute), false);
            if (customAttributes.Length > 0)
            {
                AssetTypeAttribute assetTypeAttribute = (AssetTypeAttribute)customAttributes[0];
                type = assetTypeAttribute.AssetType;
            }
            Object obj = null;
            if (!string.IsNullOrEmpty(assetguiid.stringValue))
            {
                string path = AssetDatabase.GUIDToAssetPath(assetguiid.stringValue);
                if (!string.IsNullOrEmpty(path))
                {
                    obj = AssetDatabase.LoadAssetAtPath(path, type);
                }
            }
            EditorGUI.BeginChangeCheck();
            obj = EditorGUI.ObjectField(refRect, new GUIContent("Reference: "), obj, type, false);
            if (EditorGUI.EndChangeCheck())
            {
                if (obj != null)
                {
                    string assetPath = AssetDatabase.GetAssetPath(obj);
                    AssetImporter atPath = AssetImporter.GetAtPath(assetPath);
                    if (string.IsNullOrEmpty(atPath.assetBundleName))
                    {
                        EditorUtility.DisplayDialog("Asset Error", "This asset has no asset bundle.", "OK");
                        return;
                    }
                    bundlename.stringValue = atPath.assetBundleName;
                    assetname.stringValue = Path.GetFileName(assetPath);
                    assetguiid.stringValue = AssetDatabase.AssetPathToGUID(assetPath);
                }
                else
                {
                    bundlename.stringValue = string.Empty;
                    assetname.stringValue = string.Empty;
                    assetguiid.stringValue = string.Empty;
                }
            }
            else if (obj != null)
            {
                if (!string.IsNullOrEmpty(assetguiid.stringValue))
                {
                    string text2 = AssetDatabase.GUIDToAssetPath(assetguiid.stringValue);
                    AssetImporter atPath2 = AssetImporter.GetAtPath(text2);
                    if (atPath2 != null && !string.IsNullOrEmpty(atPath2.assetBundleName))
                    {
                        bundlename.stringValue = atPath2.assetBundleName;
                        assetname.stringValue = Path.GetFileName(text2);
                    }
                }
            }
            else if (obj == null)
            {
                string _bundlename = bundlename.stringValue;
                string _assetname = assetname.stringValue;
                if (!string.IsNullOrEmpty(_bundlename) && !string.IsNullOrEmpty(_assetname))
                {
                    string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(_bundlename, Path.GetFileNameWithoutExtension(_assetname));
                    string path = null;
                    if (Path.HasExtension(_assetname))
                    {
                        string extension = Path.GetExtension(_assetname);
                        foreach (string t in assetPaths)
                        {
                            if (Path.GetExtension(t) == extension)
                            {
                                path = t;
                                break;
                            }
                        }
                    }
                    else if (assetPaths.Length > 0)
                    {
                        path = assetPaths[0];
                    }
                    if (path != null)
                    {
                        assetguiid.stringValue = AssetDatabase.AssetPathToGUID(path);
                    }
                }

            }
            GUI.enabled = (obj == null);
            EditorGUI.PropertyField(bundlerRect, bundlename, new GUIContent("Bundle:"));
            EditorGUI.PropertyField(assetRect, assetname, new GUIContent("Asset:"));
            GUI.enabled = true;
            EditorGUI.indentLevel--;
            EditorGUI.EndProperty();
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            return 4f * EditorGUIUtility.singleLineHeight;
        }
    }
}

